<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="name" clearable class="filter-item" style="width: 160px;" placeholder="请输入商品名称" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="find()">查找</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="commodity" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      

      <el-table-column align="center" label="商品ID" prop="commodity_id" />

      <el-table-column align="center" min-width="100" label="商品名称" prop="commodity_name" />

      <el-table-column align="center" property="iconUrl" label="商品图片路径" prop="commodity_img">
      </el-table-column>

      <el-table-column align="center" label="商品价格" prop="commodity_price" />

      <el-table-column align="center" label="商品库存" prop="commodity_inventory"></el-table-column>
	  
	  <el-table-column align="center" label="商品样式" prop="commodity_specification"></el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button type="primary" size="mini" @click="">编辑</el-button> -->
          <el-button type="danger" size="mini" @click="del(scope.row.commodity_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
	<button @click="first()">首页</button>
	<button @click="top()">上一页</button>
	<span>第{{this.index}}页/共{{this.pageEnd}}页</span>
	<button @click="down()">下一页</button>
	<button @click="last()">最后一页</button><br />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
  .goods-detail-box img {
    width: 100%;
  }
</style>

<script>
import { listGoods, deleteGoods,sel } from '@/api/goods'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import axios from 'axios'

export default {
  name: 'GoodsList',
  components: { BackToTop, Pagination },
  data() {
    return {
	  commodity:[],//商品列
	  index:1,//起始页
	  pageEnd:0,
	  count:0,
	  name:'',
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        goodsSn: undefined,
        name: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      goodsDetail: '',
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
	this.getCommodity()
  },
  methods: {
	  getCommodity(){
	  	axios.get('http://localhost:8088/CommodityAdmin/Page/'+this.index).then(res =>{
			console.log(res)
			this.commodity = res.data.data.date
			this.pageEnd = res.data.data.pageEnd
			this.count = res.data.data.count
			
		})
	  				
	  },
	  find(){
		  axios.get('http://localhost:8088/CommodityAdmin/like/'+this.name+'/'+this.index).then(res =>{
			  
			  this.commodity = res.data.data.date
		  })
	  },
	  del(id){
		  if(confirm("是否删除")==true){
			  axios.get('http://localhost:8088/CommodityAdmin/del/'+id).then(res =>{
			  			  console.log(res)
			  			  if(res.data.code==200){
			  				  alert("删除成功")
							  this.$router.go(0)
			  			  }else{
			  				  alert("删除失败")
			  			  }
			  			  
			  })
		  }else{
			  console.log("删除失败")
		  }
		  
	  },
	  first(){
	  	this.index=1;
	  	this.getCommodity()
	  },
	  top(){
	  	this.index-=1;
	  	if(this.index<1){
	  		this.index=1;
	  		alert("已经是第一页了")
	  	}
	  	
	  	this.getUser();
	  },
	  down(){
	  	this.index+=1;
	  	if(this.index>this.pageEnd){
	  		this.index=this.pageEnd;
	  		alert("已经是最后一页了")
	  	}
	  	this.getCommodity();
	  },
	  last(){
	  	this.index = this.pageEnd
	  	this.getCommodity()()
	  },
    getList() {
      this.listLoading = true
      listGoods(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/goods/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/goods/edit', query: { id: row.id }})
    },
    showDetail(detail) {
      this.goodsDetail = detail
      this.detailDialogVisible = true
    },
    handleDelete(row) {
      deleteGoods(row).then(response => {
        this.$notify.success({
          title: '成功',
          message: '删除成功'
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['商品ID', '商品编号', '名称', '专柜价格', '当前价格', '是否新品', '是否热品', '是否在售', '首页主图', '宣传图片列表', '商品介绍', '详细介绍', '商品图片', '商品单位', '关键字', '类目ID', '品牌商ID']
        const filterVal = ['id', 'goodsSn', 'name', 'counterPrice', 'retailPrice', 'isNew', 'isHot', 'isOnSale', 'listPicUrl', 'gallery', 'brief', 'detail', 'picUrl', 'goodsUnit', 'keywords', 'categoryId', 'brandId']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '商品信息')
        this.downloadLoading = false
      })
    }
  }
}
</script>
